/*
 * This file is part of Cadmium.
 * Copyright (C) 2007-2010 Xavier Clerc.
 *
 * Cadmium is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Cadmium is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package fr.x9c.cadmium.support.values;

import java.util.Arrays;
import java.util.Iterator;

import fr.x9c.cadmium.kernel.Block;
import fr.x9c.cadmium.kernel.Value;

/**
 * This class defines values of the <i>'a array</i> type.
 *
 * @param <T> type of array elements
 *
 * @author <a href="mailto:cadmium@x9c.fr">Xavier Clerc</a>
 * @version 1.0
 * @since 1.0
 */
public final class Array<T extends ToValue> implements ToValue, Iterable<T> {

    /** The actual value. */
    private final T[] value;

    /**
     * Constructs an array.
     * @param that array (not copied) - should not be <tt>null</tt>
     */
    public Array(final T[] that) {
        assert that != null : "null that";
        this.value = that;
    } // end constructor(T[])

    /**
     * {@inheritDoc}
     */
    @Override
    public Iterator<T> iterator() {
        return Arrays.asList(this.value).iterator();
    } // end method 'iterator()'

    /**
     * {@inheritDoc}
     */
    @Override
    public Value toValue() {
        final int len = this.value.length;
        final Block res = Block.createBlock(len, 0);
        boolean allFloats = true;
        for (int i = 0; i < len; i++) {
            final Value v = this.value[i].toValue();
            final boolean isFloat = v.isBlock()
                && (v.asBlock().getTag() == Block.DOUBLE_TAG);
            allFloats = allFloats && isFloat;
            res.set(i, v);
        } // end for
        if (allFloats && (len > 0)) {
            final Block floatRes = Block.createDoubleArray(len);
            for (int i = 0; i < len; i++) {
                floatRes.setDouble(i, res.get(i).asBlock().asDouble());
            } // end for
            return Value.createFromBlock(floatRes);
        } else {
            return Value.createFromBlock(res);
        } // end if/else
    } // end method 'toValue()'

    /**
     * {@inheritDoc}
     */
    @Override
    public int hashCode() {
        return Arrays.hashCode(this.value);
    } // end method 'hashCode()'

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean equals(final Object obj) {
        if (obj instanceof Array) {
            final Array<?> that = (Array<?>) obj;
            return Arrays.equals(this.value, that.value);
        } else {
            return false;
        } // end if/else
    } // end method 'equals(Object)'

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("[|");
        boolean first = true;
        for (T e : this.value) {
            if (first) {
                first = false;
            } else {
                sb.append("; ");
            } // end if/else
            sb.append(e.toString());
        } // end for
        sb.append("|]");
        return sb.toString();
    } // end method 'toString()'

} // end class 'Array'
